package com.lysj.bhp.manager.controller;


import com.lysj.bhp.manager.base.ManagerContent;
import com.lysj.bhp.manager.domain.Privilege;
import com.lysj.bhp.manager.domain.Role;
import com.lysj.bhp.manager.domain.Staff;
import com.lysj.bhp.util.db.LongSetAndStringConvertUtil;
import com.lysj.bhp.util.json.GsonUtil;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.util.web.SelectItem;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("admin/role")
public class RoleController extends ManagerContent {

    @GetMapping("list")
    public Resp list(Role model) {
        model.setStatus(1);
        Specification specification = ParamUtil.createSpecification(model);
        List<Role> roleList = roleRepository.findAll(specification, new Sort(Sort.Direction.ASC, "roleId"));
        return new Resp(roleList);
    }

    @PostMapping("add")
    public Resp add(Role model) {
        if (ParamUtil.isBlack(model.getName())) {
            return new Resp(Resp.PARAM_ERROR, "请输入角色名称!");
        }
        model.setStatus(1); //设定角色状态为1,正常
        roleRepository.save(model);
        return new Resp("添加成功,请给角色分配系统权限!");
    }

    @PostMapping("edit")
    public Resp edit(Role model) {
        if (ParamUtil.isBlack(model.getRoleId())) {
            return new Resp(Resp.PARAM_ERROR, "请选择角色");
        }
        Role role = roleRepository.findOne(model.getRoleId());
        if (role == null) {
            return new Resp(Resp.PARAM_ERROR, "角色错误!");
        }
        if (ParamUtil.isBlack(model.getName())) {
            return new Resp(Resp.PARAM_ERROR, "请输入角色名称");
        }
        role.setName(model.getName());
        roleRepository.save(role);
        return new Resp("修改成功");
    }

    @PostMapping("remove")
    public Resp remove(Role model) {
        if (ParamUtil.isBlack(model.getRoleId())) {
            return new Resp(Resp.PARAM_ERROR, "请选择角色");
        }
        Role role = roleRepository.findOne(model.getRoleId());
        if (role == null) {
            return new Resp(Resp.PARAM_ERROR, "角色错误!");
        }
        role.setStatus(2);
        roleRepository.save(role);
        return new Resp("删除成功");
    }

    @GetMapping("select_item")
    public Resp selectItem() {
        List<SelectItem> selectItems = roleRepository.selectItems();
        return new Resp(selectItems);
    }


    @PostMapping("/editPriv")
    public Resp editPriv(Long roleId, String privIds) {
        if (ParamUtil.isBlack(roleId)) {
            return new Resp(Resp.PARAM_ERROR, "请选择角色!");
        }
        Role role = roleRepository.findOne(roleId);
        if (role == null) {
            return new Resp(Resp.PARAM_ERROR, "请选择角色!");
        }
        Set<Long> privIdSet = new HashSet<>();
        if (!ParamUtil.isBlack(privIds)) {
            privIdSet = LongSetAndStringConvertUtil.string2LongSet(privIds);
        }
        //修改角色对应的权限
        role.setPrivIds(LongSetAndStringConvertUtil.longSet2String(privIdSet));
        List<Privilege> privilegeList = privilegeRepository.findAll(privIdSet);
        role.setPrivJson(GsonUtil.toJson(privilegeList));
        //修改员工所属角色的基本权限
        List<Staff> staffList = staffRepository.findByRoleId(roleId);
        for (Staff staff : staffList) {
            staff.setPrivIds(LongSetAndStringConvertUtil.longSet2String(privIdSet));
            staff.setPrivJson(GsonUtil.toJson(privilegeList));
        }
        staffRepository.save(staffList);
        roleRepository.save(role);
        return new Resp("修改角色权限成功!");
    }
}
